<?php

/**
 * 分销中心（提现） - AM
 * 版本：2.0
 */

global $_W, $_GPC;
$openid = m('user')->getOpenid();

if ($_W['isajax']) {

	$member = m('member')->getMember($openid);

	$commissionPrice = $this->model->getCommissionRecordPrice($openid, array('ok'));
	$commissionPriceOk = !empty($commissionPrice['ok']) ? number_format($commissionPrice['ok'], 2) : 0;

	$cansettle = $commissionPriceOk > 0 && $commissionPriceOk >= floatval($this->set['withdraw']);

	$paySet = m('common')->getSysset('pay');

	if ($_W['ispost']) {

		if(!$cansettle){
			show_json(0, '很抱歉，您不满足可以提现的条件!');
		}

		if($commissionPriceOk <= 0){
			show_json(0, '不存在可以提现的佣金!');
		}

		$commissionPriceOk = $this->model->getWithdrawals($openid, $_GPC['type']);

		$returnurl = urlencode($this->createMobileUrl('member/withdraw'));
		$infourl = $this->createMobileUrl('member/info', array('returnurl' => $returnurl));

		$this->model->sendMessage($openid, array('commission' => $commissionPriceOk, 'type' => intval($_GPC['type']) == 1 ? '微信' : '余额'), TM_COMMISSION_APPLY);
		show_json(1, '已提交,请等待审核!');
	}

	$returnurl = urlencode($this->createPluginMobileUrl('commission/apply'));
	$infourl = $this->createMobileUrl('member/info', array('returnurl' => $returnurl));

	show_json(1, array('commission_ok' => $commissionPriceOk, 'cansettle' => $cansettle, 'member' => $member, 'set' => $this->set, 'infourl' => $infourl, 'noinfo' => empty($member['realname']), 'payset' => $paySet));

}

include $this->template('apply');